package com.lengxf.netty.config.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@ChannelHandler.Sharable
public class UserHandlerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof TextWebSocketFrame textFrame) {
            log.info("接收到信息: {}", textFrame.text());
            //TODO doBusiness
        } else if (msg instanceof PongWebSocketFrame) {
            log.info("WebSocket Client received pong...");
        } else if (msg instanceof CloseWebSocketFrame) {
            log.info("WebSocket Client received closing...");
            ctx.channel().close();
        }
        super.channelRead(ctx, msg);
    }

}